/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.kaoqin.service.impl;

import me.zhengjie.modules.kaoqin.domain.Kaoqin;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.kaoqin.repository.KaoqinRepository;
import me.zhengjie.modules.kaoqin.service.KaoqinService;
import me.zhengjie.modules.kaoqin.service.dto.KaoqinDto;
import me.zhengjie.modules.kaoqin.service.dto.KaoqinQueryCriteria;
import me.zhengjie.modules.kaoqin.service.mapstruct.KaoqinMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author wzy
* @date 2024-11-17
**/
@Service
@RequiredArgsConstructor
public class KaoqinServiceImpl implements KaoqinService {

    private final KaoqinRepository kaoqinRepository;
    private final KaoqinMapper kaoqinMapper;

    @Override
    public PageResult<KaoqinDto> queryAll(KaoqinQueryCriteria criteria, Pageable pageable){
        Page<Kaoqin> page = kaoqinRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(kaoqinMapper::toDto));
    }

    @Override
    public List<KaoqinDto> queryAll(KaoqinQueryCriteria criteria){
        return kaoqinMapper.toDto(kaoqinRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public KaoqinDto findById(Integer id) {
        Kaoqin kaoqin = kaoqinRepository.findById(id).orElseGet(Kaoqin::new);
        ValidationUtil.isNull(kaoqin.getId(),"Kaoqin","id",id);
        return kaoqinMapper.toDto(kaoqin);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Kaoqin resources) {
        kaoqinRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Kaoqin resources) {
        Kaoqin kaoqin = kaoqinRepository.findById(resources.getId()).orElseGet(Kaoqin::new);
        ValidationUtil.isNull( kaoqin.getId(),"Kaoqin","id",resources.getId());
        kaoqin.copy(resources);
        kaoqinRepository.save(kaoqin);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            kaoqinRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<KaoqinDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (KaoqinDto kaoqin : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("课程号", kaoqin.getClassid());
            map.put("课程名", kaoqin.getClassname());
            map.put("学号", kaoqin.getStudentid());
            map.put("学生姓名", kaoqin.getStudentname());
            map.put("成绩", kaoqin.getGrade());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}